<uieip type="reg" value="resource,data"></uieip>
<script runat="server" src="/handler/js/rsa.js"></script>
<script runat="server">
    response.contentType('application/json');

    var res = get_adp_info();
    if (res.error != 0) {
        response.body(JSON.stringify(res));
    } else {
        response.body(JSON.stringify(res.value));
    }

    //{"motherboard":"AA20240906512G733413","cpu":"B8:EE:65:2F:13:C3","sso":"http://user.yqsite.com/","eip":"http://eip.greenman.com.cn/"}
    function get_adp_info() {
        var msg = request.body(true);
        var res = resource.actionMsgByString(msg);
        if (res.error != 0) {
            return res;
        }

        res = resource.rsaDecryptCSharp(res.content, privateKey);
        if (res.error != 0) {
            return res;
        }

        var dd = JSON.parse(res.value);
        var motherboard = dd.motherboard;
        var sso = dd.sso;
        var eip = dd.eip;
        var cpu = dd.cpu;

        // motherboard = "7BVMPP4CQ4WMQAF8QDJ4";
        // cpu = "00:1C:42:B3:70:D8";

        res = data.select("yqbase", "licAdp", "motherboard='" + motherboard + "' and cpu='" + cpu + "'", "");
        if (res.error != 0) {
            return res;
        }
        if (res.rows.length == 0) {
            return {
                error: 100,
                info: "adp not found"
            }
        }

        var row = res.rows[0];
        var ip = request.ip();

        // 检查是否已注销
        if (row.isdel === true) {
            update_link(row.sid, sso, eip, 101, ip)
            return {
                error: 101,
                info: "服务注销状态，请登录 www.yqsite.com 续期服务。"
            }
        }

        //row.duedate = "2025-12-16 00:00:00";
        // 检查是否已到期
        if (row.duedate) {
            var duedate = new Date(row.duedate);
            var currentDate = new Date();
            if (currentDate > duedate) {
                var duedateStr = row.duedate.split(' ')[0]; // 只取日期部分
                update_link(row.sid, sso, eip, 102, ip)
                return {
                    error: 102,
                    info: "服务已过期[" + duedateStr + "]，请登录 www.yqsite.com 续期服务。"
                }
            }
        }

        row["dueDate"] = row.duedate.split(' ')[0]; // 只取日期部分
        row["appDate"] = row.appdate.split(' ')[0]; // 只取日期部分
        delete row.duedate;
        delete row.appdate;

        update_link(row.sid, sso, eip, 0, ip)
        
        row["error"] = 0;
        return {
            error: 0,
            info: "success",
            value: row
        }
    }

    function update_link(sid, sso, eip, error, ip) {
        var checktime = resource.timeToISOString(new Date());

        var res = data.action("yqbase", "licAdp", [{
            "action": "upp_",
            row: {
                "sid": sid,
                "checkcorrect": error,
                "checktime": checktime,
                "ip": ip
            }
        }]);
        if (res.error != 0) {
            return res;
        }

        res = data.action("yqbase", "licAdpLog", [{
            "action": "add",
            row: {
                "pid": sid,
                "checkcorrect": error,
                "checktime": checktime,
                "sso": sso,
                "eip": eip,
                "ip": ip
            }
        }]);

        return res;
    }
</script>